package com.hgyc.mom.mes.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hgyc.mom.common.util.poi.ExcelUtil;
import com.hgyc.mom.common.vo.Result;
import com.hgyc.mom.core.mybatis.QueryWrapperUtil;
import com.hgyc.mom.mes.entity.MesPackageRule;
import com.hgyc.mom.mes.service.MesPackageRuleService;
import com.hgyc.mom.mes.vo.MesPackageRuleParam;
import com.hgyc.mom.mes.vo.MesPackageRuleVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Arrays;
import java.util.List;

/**
 * 包装规则Controller
 *
 * @author fwj
 * @since 2025-06-30
 */
@Tag(name = "包装规则控制器", description = "包装规则控制器")
@RestController
@RequestMapping("/mes/package/rule")
public class MesPackageRuleController {
    @Autowired
    private MesPackageRuleService mesPackageRuleService;

    @Operation(summary = "查询包装规则列表", description = "查询包装规则列表")
    @PostMapping("/pageList")
    public Result<IPage<MesPackageRule>> pageList(@Parameter(description = "查询参数") @RequestBody MesPackageRuleParam mesPackageRuleParam) {
        Page<MesPackageRule> page = new Page<>(mesPackageRuleParam.getPage(), mesPackageRuleParam.getPageSize());
        MesPackageRule mesPackageRule = new MesPackageRule();
        BeanUtils.copyProperties(mesPackageRuleParam, mesPackageRule);
        QueryWrapper<MesPackageRule> wrapper = QueryWrapperUtil.build(mesPackageRule);
        IPage<MesPackageRule> list = mesPackageRuleService.page(page, wrapper);
        return Result.success(list);
    }


    @Operation(summary = "获取包装规则详细信息", description = "获取包装规则详细信息")
    @GetMapping(value = "/{id}")
    public Result getInfo(@Parameter(description = "包装规则ID") @PathVariable("id") Long id) {
        return Result.success(mesPackageRuleService.getById(id));
    }
    @Operation(summary = "新增包装规则", description = "新增包装规则")
    @PostMapping
    public Result<?> save(@Parameter(description = "包装规则") @RequestBody MesPackageRuleVO mesPackageRuleVO) {
        mesPackageRuleService.savePacakageRule(mesPackageRuleVO);
        return Result.success();
    }

    @Operation(summary = "修改包装规则", description = "修改包装规则")
    @PutMapping
    public Result<?> update(@Parameter(description = "包装规则") @RequestBody MesPackageRuleVO mesPackageRuleVO) {
        mesPackageRuleService.updatePacakageRule(mesPackageRuleVO);
        return Result.success();
    }

    @Operation(summary = "删除包装规则", description = "删除包装规则")
    @DeleteMapping
    public Result<?> remove(@Parameter(description = "删除ID") @RequestParam("ids") Long[] ids) {
        mesPackageRuleService.removePacakageRule(Arrays.asList(ids));
        return Result.success();
    }

    @Operation(summary = "下载导入模版", description = "下载导入模版")
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil<MesPackageRule> util = new ExcelUtil<>(MesPackageRule.class);
        util.importTemplateExcel(response, "包装规则");
    }

    @Operation(summary = "导出包装规则列表", description = "导出包装规则列表")
    @PostMapping("/export")
    public void export(HttpServletResponse response, MesPackageRuleParam mesPackageRuleParam) {
        MesPackageRule mesPackageRule = new MesPackageRule();
        BeanUtils.copyProperties(mesPackageRuleParam, mesPackageRule);
        QueryWrapper<MesPackageRule> wrapper = QueryWrapperUtil.build(mesPackageRule);
        List<MesPackageRule> list = mesPackageRuleService.list(wrapper);
        ExcelUtil<MesPackageRule> util = new ExcelUtil<>(MesPackageRule.class);
        util.exportExcel(response, list, "包装规则数据");
    }

    @Operation(summary = "导入包装规则列表", description = "导入包装规则列表")
    @PostMapping("/import")
    public Result<?> importData(MultipartFile file, boolean updateSupport) throws Exception {
        ExcelUtil<MesPackageRule> util = new ExcelUtil<>(MesPackageRule.class);
        List<MesPackageRule> mesPackageRuleList = util.importExcel(file.getInputStream());
        String message = mesPackageRuleService.importData(mesPackageRuleList, updateSupport);
        return Result.success(message);
    }
}
